package com.touzbi.hss.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import com.touzbi.hibernate.util.HibernateUtil;
import com.touzbi.hss.beans.Patient;
import com.touzbi.hss.bo.PatientListBO;
import com.touzbi.hss.bo.SearchBO;

/**
 * Servlet implementation class SearchServlet
 */
@WebServlet("/search")
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String SEARCH_JSP = "/WEB-INF/list.jsp";
	private static final String RESULT_QUERY_LIST = "listPatient";
	private static final String LIST_COLUMN = "listColumn";
	private static final String LIST_TITLE_ATT = "title";
	private static final Object LIST_TITLE = "Results";
	private static final String SEARCH_BO = "search";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		SearchBO searchBO = new SearchBO(session);
		PatientListBO patientBO = new PatientListBO(session);

		List<Patient> result = searchBO.getQueryResult(req);
		List<String> listColumn = patientBO.getlistColumn();

		session.getTransaction().commit();

		req.setAttribute(RESULT_QUERY_LIST, result);
		req.setAttribute(LIST_COLUMN, listColumn);
		req.setAttribute(LIST_TITLE_ATT, LIST_TITLE);
		req.setAttribute(SEARCH_BO, searchBO);

		this.getServletContext().getRequestDispatcher(SEARCH_JSP)
				.forward(req, resp);
	}

}
